//
//  XUtilsMacros.h
//  XUtils
//
//  Created by anmin on 2017/6/30.
//  Copyright © 2017年 puhui. All rights reserved.
//

//  方法失效
#import <Foundation/Foundation.h>

#define XUtilsDeprecated(_msg) __attribute__((deprecated(_msg)))

typedef void(^NoParamBlock)(void);
typedef void(^NormalSuccessBlock)(_Nullable id responseObject);
typedef void(^NormalFailureBlock)(NSError * _Nullable error);

#pragma mark - weakify

#ifndef    weakify
#if __has_feature(objc_arc)

#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x; \
_Pragma("clang diagnostic pop")

#else

#define weakify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
autoreleasepool{} __block __typeof__(x) __block_##x##__ = x; \
_Pragma("clang diagnostic pop")

#endif
#endif

#ifndef    strongify
#if __has_feature(objc_arc)

#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __weak_##x##__; \
_Pragma("clang diagnostic pop")

#else

#define strongify( x ) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
try{} @finally{} __typeof__(x) x = __block_##x##__; \
_Pragma("clang diagnostic pop")

#endif
#endif

#pragma mark ======= singleton shared instance =======

// .h文件
#define SingletonH(name) + (instancetype)shared##name;
// .m文件
#define SingletonM(name)\
static id _instance; \
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}

#pragma mark - Color

#define RGBA_COLOR(r, g, b, a) [UIColor colorWithRed:r/255. green:g/255. blue:b/255. alpha:a]

#define RGB_COLOR(r, g, b) [UIColor colorWithRed:r/255. green:g/255. blue:b/255. alpha:1]

#define HEXA_COLOR(hexValue, AlphaValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:AlphaValue]

#define HEX_COLOR(hexValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:1.0]


#pragma mark - System


#define DEVICE_SCALE [UIScreen mainScreen].scale
#define DEVICE_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)
#define DEVICE_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)
#define DEVICE_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] doubleValue]
#define USER_DEFAULTS [NSUserDefaults standardUserDefaults]

#pragma mark - DEVICE

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA (DEVICE_SCALE >= 2.0)
#define DEVICE_MAX_LENGTH (MAX(DEVICE_WIDTH, DEVICE_HEIGHT))
#define DEVICE_MIN_LENGTH (MIN(DEVICE_WIDTH, DEVICE_HEIGHT))

//#define IS_IPHONE_X 

#pragma mark - UI

#pragma mark - KEYS

#pragma mark - other

#define Show_Tool_view_duration 0.25

#define ToStringValue(x) @#x
